package com.winning.pmph.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.winning.pmph.entity.*;
import com.winning.pmph.mapper.KnowledgeHistoryMapper;
import org.apache.commons.collections.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wu_di@winning.com.cn
 * @date 2020/11/25
 */
@Component
@Transactional
public class KnowledgeHistoryService extends ServiceImpl<KnowledgeHistoryMapper, KnowledgeHistory> {
    @Resource
    private ReleasedKnowledgeFieldService releasedKnowledgeFieldService;
    @Resource
    private KnowledgeFieldHistoryService knowledgeFieldHistoryService;

    public KnowledgeHistory add(ReleasedKnowledge releasedKnowledge) {
        KnowledgeHistory knowledgeHistory = getKnowledgeHistory(releasedKnowledge);
        save(knowledgeHistory);

        if (CollectionUtils.isEmpty(releasedKnowledge.getKnowledgeFieldList())) {
            List<KnowledgeField> releasedKnowledgeFields = Lists.newArrayList();
            releasedKnowledgeFieldService.lambdaQuery()
                    .eq(ReleasedKnowledgeField::getKnowledgeId, releasedKnowledge.getId())
                    .orderByAsc(ReleasedKnowledgeField::getId).list().forEach(releasedKnowledgeFields::add);
            releasedKnowledge.setKnowledgeFieldList(releasedKnowledgeFields);
        }
        List<KnowledgeFieldHistory> fieldList = new ArrayList<>();
        for (KnowledgeField field : releasedKnowledge.getKnowledgeFieldList()) {
            KnowledgeFieldHistory history = getKnowledgeFieldHistory(knowledgeHistory, field);
            fieldList.add(history);
        }
        knowledgeFieldHistoryService.saveOrUpdateBatch(fieldList);

        return knowledgeHistory;
    }

    @NotNull
    private KnowledgeFieldHistory getKnowledgeFieldHistory(KnowledgeHistory knowledgeHistory, KnowledgeField field) {
        KnowledgeFieldHistory history = new KnowledgeFieldHistory();
        history.setKnowledgeHistoryId(knowledgeHistory.getId());
        history.setKnowledgeFieldId(field.getId());
        history.setKnowledgeId(field.getKnowledgeId());
        history.setName(field.getName());
        history.setDisplayName(field.getDisplayName());
        history.setKind(field.getKind());
        history.setProperty(field.getProperty());
        history.setProperty(field.getProperty());
        history.setKindValue(field.getKindValue());
        history.setRequired(field.getRequired());
        history.setHidden(field.getHidden());
        history.setSource(field.getSource());
        history.setKindValue(field.getKindValue());
        history.setWordsCount(field.getWordsCount());
        history.setKnowledgeLibFieldId(field.getKnowledgeLibFieldId());
        history.setKindValue(field.getKindValue());
        return history;
    }

    @NotNull
    private KnowledgeHistory getKnowledgeHistory(ReleasedKnowledge releasedKnowledge) {
        KnowledgeHistory knowledgeHistory = new KnowledgeHistory();
        knowledgeHistory.setKnowledgeId(releasedKnowledge.getId());
        knowledgeHistory.setName(releasedKnowledge.getName());
        knowledgeHistory.setVersion(releasedKnowledge.getVersion());
        knowledgeHistory.setKnowledgeLibId(releasedKnowledge.getKnowledgeLibId());
        knowledgeHistory.setIdentify(releasedKnowledge.getIdentify());
        knowledgeHistory.setDeleted(releasedKnowledge.getDeleted());
        return knowledgeHistory;
    }
}
